package studio.raptor.databus.graph.common;

import java.io.IOException;
import org.apache.http.HttpHost;
import org.apache.http.conn.routing.HttpRoute;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;

/**
 * @author jack
 * @since 1.0
 */
public class HttpConnectionManager {



  private static PoolingHttpClientConnectionManager cm;
  private static CloseableHttpClient httpClient;

  /**
   * 最大连接数
   */
  public final static int MAX_TOTAL_CONNECTIONS = 800;
  /**
   * 获取连接的最大等待时间
   */
  public final static int WAIT_TIMEOUT = 60000;
  /**
   * 每个路由最大连接数
   */
  public final static int MAX_ROUTE_CONNECTIONS = 400;
  /**
   * 连接超时时间
   */
  public final static int CONNECT_TIMEOUT = 10000;
  /**
   * 读取超时时间
   */
  public final static int READ_TIMEOUT = 10000;

  static {
    cm = new PoolingHttpClientConnectionManager();
    // 将最大连接数增加到200
    cm.setMaxTotal(50);
    // 将每个路由基础的连接增加到20
    cm.setDefaultMaxPerRoute(20);
    //将目标主机的最大连接数增加到50
    HttpHost localhost = new HttpHost("http://127.0.0.1", 9090);
    cm.setMaxPerRoute(new HttpRoute(localhost), 50);

    httpClient = HttpClients.custom()
        .setConnectionManager(cm)
        .build();

  }

  public static CloseableHttpClient getHttpClient() {
    return httpClient;
  }

  public static void close() throws IOException {
    httpClient.close();
  }


}
